/*
 * organizationdao_dboperator.cpp
 *
 *  Created on: 2013-10-23
 *      Author: hadeswang
 */

#include "organizationdao_dboperator.h"
#include "c2cplatform/component/dal/dal_set.h"
#include "c2cent/library/log/CWWLog2.h"
#include "erp/comm/erp_errno_define.h"
#include "erp/comm/erp_macro_define.h"

using namespace erp::organization::dao;
using namespace erp::comm;
using namespace c2cplatform::component::dal;
using namespace c2cent::library::wwlog;

#define ROLE_NAME_ORGANIZATION "erp_yun_organization"
#define TABLE_NAME_BRANCH "T_Branch_"

COrganizationDBOperator::COrganizationDBOperator()
{
}

COrganizationDBOperator::~COrganizationDBOperator()
{
}

int32_t COrganizationDBOperator::ModifyBranch(
		const CCntlInfo& rCntlInfo,
		const std::string& strMachineKey,
		const std::string& strSource,
		const uint32_t dwSceneId,
		const erp::organization::ddo::CBranchDdo& oBranch)
{
	int32_t iRetCode = 0;
	uint16_t wUseSetId = 0;

	CONNECT_WITH_DALSET(m_oStorage, rCntlInfo.getCmdId(),
				rCntlInfo.getOperatorUin(), ROLE_NAME_ORGANIZATION,
				TABLE_NAME_BRANCH, wUseSetId, rCntlInfo, strMachineKey, strSource,
				dwSceneId);

	std::stringstream ssSql;

	SQL_INSERT_TABLE(ssSql, wUseSetId, rCntlInfo.getOperatorUin(), ROLE_NAME_ORGANIZATION, TABLE_NAME_BRANCH);
	SQL_SET_INT_FIELD(ssSql, BranchSysNo, oBranch);
	SQL_SET_INT_FIELD(ssSql, ParentBranchSysNo, oBranch);
	SQL_SET_STRING_FIELD(ssSql, FullName, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, BranchName, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, BranchID, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, Comment, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, PyCode, oBranch, m_oStorage);
	SQL_SET_INT_FIELD(ssSql, PrebuyPriceLevel, oBranch);
	SQL_SET_STRING_FIELD(ssSql, Contact, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, Phone, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, ZipCode, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, Address, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, BankAccountName, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, TaxID, oBranch, m_oStorage);
	SQL_SET_STRING_FIELD(ssSql, AccoutName, oBranch, m_oStorage);
	SQL_SET_INT_FIELD(ssSql, AcountMode, oBranch);
	SQL_SET_PROPERTY_FIELD(ssSql, BranchProperty, oBranch);
	SQL_SET_INT_FIELD(ssSql, BranchStatus, oBranch);
	SQL_SET_INT_FIELD_VALUE(ssSql, CreatorSysNo, rCntlInfo.getOperatorUid());
	SQL_SET_TIME_FIELD_NOW(ssSql, RowCreatDate);
	SQL_SET_INT_FIELD_VALUE(ssSql, ModifierSysNo, rCntlInfo.getOperatorUid());
	SQL_SET_TIME_FIELD_NOW_END(ssSql, RowModifyDate);
	SQL_DUPLICATE_UPDATE(ssSql);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, ParentBranchSysNo, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, FullName, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, BranchName, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, BranchID, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, Comment, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, PyCode, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, PrebuyPriceLevel, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, Contact, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, Phone, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, ZipCode, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, Address, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, BankAccountName, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, TaxID, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, AccoutName, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, AcountMode, oBranch);
	SQL_SET_PROPERTY_FIELD(ssSql, BranchProperty, oBranch);
	SQL_SET_FIELD_BEFORE_VALUE(ssSql, BranchStatus, oBranch);
	SQL_SET_INT_FIELD_VALUE(ssSql, ModifierSysNo, rCntlInfo.getOperatorUid());
	SQL_SET_TIME_FIELD_NOW_END(ssSql, RowModifyDate);

	DB_UPDATE(m_oStorage, ssSql, iRetCode, rCntlInfo, strMachineKey, strSource, dwSceneId);

	return iRetCode;
}

int32_t COrganizationDBOperator::GetBranch(
		const CCntlInfo& rCntlInfo,
		const std::string& strMachineKey,
		const std::string& strSource,
		const uint32_t dwSceneId,
		const std::set<uint64_t> & setBranchId,
		std::map<uint64_t,erp::organization::ddo::CBranchDdo> & mapBranch)
{
	int32_t iRetCode = 0;
	uint16_t wUseSetId = 0;

	CONNECT_WITH_DALSET(m_oStorage, rCntlInfo.getCmdId(),
				rCntlInfo.getOperatorUin(), ROLE_NAME_ORGANIZATION,
				TABLE_NAME_BRANCH, wUseSetId, rCntlInfo, strMachineKey, strSource,
				dwSceneId);

	std::stringstream ssSql;

	SQL_SELECT_TABLE(ssSql, wUseSetId, rCntlInfo.getOperatorUin(), ROLE_NAME_ORGANIZATION, TABLE_NAME_BRANCH,
			"FBranchSysNo,FParentBranchSysNo,FFullName,FBranchName,FBranchID,FComment,FPyCode,FPrebuyPriceLevel,"
			"FContact,FPhone,FZipCode,FAddress,FBankAccountName,FBankAccount,FTaxID,FAccoutName,FAcountMode,"
			"FBranchProperty,FBranchStatus,FCreatorSysNo,UNIX_TIMESTAMP(FRowCreatDate) as FRowCreatDate,FModifierSysNo,UNIX_TIMESTAMP(FRowModifyDate) as FRowModifyDate");
	SQL_WHERE_BEGIN(ssSql);
	SQL_WHERE_FIELD_SET(ssSql,BranchSysNo,setBranchId);

	c2cent::storage::IResultSet& oRs=DB_SELECT(m_oStorage, ssSql);

	DB_SELECT_RS_CHECK(oRs, m_oStorage, ssSql, iRetCode, rCntlInfo, strMachineKey, strSource, dwSceneId);

    do{
    	erp::organization::ddo::CBranchDdo oBranchDdo;

    	RS_GET_INT_FIELD(oRs, BranchSysNo, oBranchDdo);
    	RS_GET_INT_FIELD(oRs, ParentBranchSysNo, oBranchDdo);
    	RS_GET_STRING_FIELD(oRs, FullName, oBranchDdo);
    	RS_GET_STRING_FIELD(oRs, BranchName, oBranchDdo);
    	RS_GET_STRING_FIELD(oRs, BranchID, oBranchDdo);
    	RS_GET_STRING_FIELD(oRs, Comment, oBranchDdo);
    	RS_GET_STRING_FIELD(oRs, PyCode, oBranchDdo);
    	RS_GET_INT_FIELD(oRs, PrebuyPriceLevel, oBranchDdo);
		RS_GET_STRING_FIELD(oRs, Contact, oBranchDdo);
		RS_GET_STRING_FIELD(oRs, Phone, oBranchDdo);
		RS_GET_STRING_FIELD(oRs, ZipCode, oBranchDdo);
		RS_GET_STRING_FIELD(oRs, Address, oBranchDdo);
		RS_GET_STRING_FIELD(oRs, BankAccountName, oBranchDdo);
		RS_GET_STRING_FIELD(oRs, TaxID, oBranchDdo);
		RS_GET_STRING_FIELD(oRs, AccoutName, oBranchDdo);
		RS_GET_INT_FIELD(oRs, AcountMode, oBranchDdo);
		RS_GET_INT_FIELD(oRs, BranchProperty, oBranchDdo);
		RS_GET_INT_FIELD(oRs, BranchStatus, oBranchDdo);
		RS_GET_INT_FIELD(oRs, CreatorSysNo, oBranchDdo);
		RS_GET_INT_FIELD(oRs, RowCreatDate, oBranchDdo);
		RS_GET_INT_FIELD(oRs, ModifierSysNo, oBranchDdo);
		RS_GET_INT_FIELD(oRs, RowModifyDate, oBranchDdo);

		mapBranch[oBranchDdo.GetBranchSysNo()]=oBranchDdo;

    }while(oRs.Next());

    oRs.Close();

	return iRetCode;
}

int32_t COrganizationDBOperator::FindBranch(
			const CCntlInfo& rCntlInfo,
			const std::string& strMachineKey,
			const std::string& strSource,
			uint32_t dwSceneId,
			const erp::organization::ddo::COrganizationFilterDdo& oFilter,
			uint32_t& dwTotal,
			std::vector<erp::organization::ddo::CBranchDdo> & vecBranch)
{
	int32_t iRetCode = 0;
	uint16_t wUseSetId = 0;

	CONNECT_WITH_DALSET(m_oStorage, rCntlInfo.getCmdId(),
				rCntlInfo.getOperatorUin(), ROLE_NAME_ORGANIZATION,
				TABLE_NAME_BRANCH, wUseSetId, rCntlInfo, strMachineKey, strSource,
				dwSceneId);

	std::stringstream ssSql,ssCount,ssList;

	SQL_SELECT_TABLE(ssCount, wUseSetId, rCntlInfo.getOperatorUin(), ROLE_NAME_ORGANIZATION, TABLE_NAME_BRANCH,
			"FBranchSysNo,FParentBranchSysNo,FFullName,FBranchName,FBranchID,FComment,FPyCode,FPrebuyPriceLevel,"
			"FContact,FPhone,FZipCode,FAddress,FBankAccountName,FBankAccount,FTaxID,FAccoutName,FAcountMode,"
			"FBranchProperty,FBranchStatus,FCreatorSysNo,UNIX_TIMESTAMP(FRowCreatDate) as FRowCreatDate,FModifierSysNo,UNIX_TIMESTAMP(FRowModifyDate) as FRowModifyDate");

	SQL_SELECT_TABLE(ssList, wUseSetId, rCntlInfo.getOperatorUin(), ROLE_NAME_ORGANIZATION, TABLE_NAME_BRANCH,
			"FBranchSysNo,FParentBranchSysNo,FFullName,FBranchName,FBranchID,FComment,FPyCode,FPrebuyPriceLevel,"
			"FContact,FPhone,FZipCode,FAddress,FBankAccountName,FBankAccount,FTaxID,FAccoutName,FAcountMode,"
			"FBranchProperty,FBranchStatus,FCreatorSysNo,UNIX_TIMESTAMP(FRowCreatDate) as FRowCreatDate,FModifierSysNo,UNIX_TIMESTAMP(FRowModifyDate) as FRowModifyDate");

	SQL_WHERE_BEGIN(ssSql);
	SQL_WHERE_FIELD_SET(ssSql,FParentBranchSysNo,oFilter.GetParentId());

	return iRetCode;
}
